<?php
/**addroom
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-21
 * Time: 16:55
 */
namespace App\Http\Controllers\Mobile\Room;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Backstage\Customers;
use App\Model\Backstage\RoomInfo;
use App\Http\Controllers\Validator\RoomInfoVail;
use App\Tool\Error;
class RoomController extends Controller
{
    /**
     *  移动端房源列表
     */
    public function AppRoomList(Request $request,RoomInfo $roomInfo){

        $where[] = ['room_status',1];
        $param = $request->all();
        //房型   普通房  优质房
        if(isset($param['type_of_house_sale']) && $param['type_of_house_sale'] !=''){
            $where[] = ['type_of_house_sale' , $param['type_of_house_sale']];
        }
        //用途
        if(isset($param['use_type']) && $param['use_type'] != ''){
            $where[] = ['use_type' , $param['use_type']];
        }

        //面积
        if(isset($param['r_acreage_begin']) && $param['r_acreage_begin'] != ''){   //开始面积
            $where[] = ['r_acreage', '>=', $param['r_acreage_begin']];
        }
        if(isset($param['r_acreage_end']) && $param['r_acreage_end'] != ''){   //结束面积
            $where[] = ['r_acreage', '<=', $param['r_acreage_end']];
        }

        //状态
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['status' , $param['status']];
        }
        //层高类型   高层  低层
        if(isset($param['housing_types']) && $param['housing_types'] != ''){
            $where[] = ['housing_types' , $param['housing_types']];
        }

//        //朝向
//        if(isset($param['r_orientation']) && $param['r_orientation'] != ''){
//            $where[] = ['r_orientation' => $param['r_orientation']];
//        }

        //楼层
        if(isset($param['floor_begin']) && $param['floor_begin'] != ''){  //开始楼层
            $where[] = ['floor', '>=', $param['floor_begin']];
        }
        if(isset($param['floor_end']) && $param['floor_end'] != ''){    //结束楼层
            $where[] = ['floor', '<=', $param['floor_end']];
        }
        //部门
        if(isset($param['r_d_id']) && $param['r_d_id'] !=''){
            $where[] = ['r_d_id',$param['r_d_id']];
        }
        //员工
        if(isset($param['source_uid']) && $param['source_uid'] != ''){
            $where[] = ['source_uid', $param['source_uid']];
        }

        //交易类型  仅租  仅售  租售
        if(isset($param['trading_type']) && $param['trading_type'] != ''){
            $where[] = ['trading_type', $param['trading_type']];
        }
//        //交易状态类型  有效  无效
        if(isset($param['trading_state_type']) && $param['trading_state_type'] != ''){
            $where[] = ['trading_state_type' ,$param['trading_state_type']];
        }

        //价格 给价格类型值 1按租价  2按售价
        $param['price_type'] = 1;  //对接数据时删除，现在做默认值测试
        if($param['price_type'] == 1){
            if(isset($param['begin_price']) && $param['begin_price'] != ''){
                $where[] = ['floor_price', '>=', $param['begin_price']];
            }
            if(isset($param['end_price']) && $param['end_price'] != ''){
                $where[] = ['floor_price', '<=', $param['end_price']];
            }
        }else{
            if(isset($param['begin_price']) && $param['begin_price'] != ''){
                $where[] = ['unit_price', '>=', $param['begin_price']];
            }
            if(isset($param['end_price']) && $param['end_price'] != ''){
                $where[] = ['unit_price', '<=', $param['end_price']];
            }
        }
        //录入时间
        if ($request->input('date_type') == 1){
            if ($param['start_time']) {
                $where[] = ['r_created_at', '>=', strtotime($param['start_time'])];
            }
            if ($param['end_time']) {
                $where[] = ['r_created_at', '<=', strtotime($param['end_time'])];
            }
        }
        //最后跟进时间
        if ($request->input('date_type') == 2){
            if ($param['start_time']) {
                $where[] = ['r_follow_time', '>=', strtotime($param['start_time'])];
            }
            if ($param['end_time']) {
                $where[] = ['r_follow_time', '<=', strtotime($param['end_time'])];
            }
        }
        //交房时间
        if ($request->input('date_type') == 3){
            if ($param['start_time']) {
                $where[] = ['making_room', '>=', strtotime($param['start_time'])];
            }
            if ($param['end_time']) {
                $where[] = ['making_room', '<=', strtotime($param['end_time'])];
            }
        }

        //建房年代
        if ($request->input('date_type') == 4){
            if ($param['start_time']) {
                $where[] = ['building_s', '>=', $param['start_time']];
            }
            if ($param['end_time']) {
                $where[] = ['building_s', '<=', $param['end_time']];
            }
        }
        //委托时间
        if($request->input('date_type') == 5){
            if($request->input('start_time')){
                $where[] = ['r_entrust_at', '>=', $param['start_time']];
            }
            if($request->input('end_time')){
                $where[] = ['r_entrust_at', '<=', $param['end_time']];
            }
        }
        //关键词智能搜索
        $keywords = '';
        if ($request->input('keywords')){
            $keywords = trim($request->input('keywords'));
        }
        $list = $roomInfo->listRoom(env('PAGE_LIMIT_ADMIN'), $where, '');
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }

        return $list;
    }

    /**
     * 移动端新增房源
     */
    public function addroom(Request $request,RoomInfo $RoomInfo, Customers $customers){
        $params = $request->except('token');
        $validator =  RoomInfoVail::validate($params);
        if (!$validator) {
            //参数不完整
            return $this->error(Error::PARAM_MISSING);
        }
        //根据房源用途不同前台传的不同类型字段值,转换成json字符串，输出时直接转换成json数组前台取值
        $json_info = $request->input('r_info','{}');
        $params['r_info'] = json_encode($json_info);
        //房源明细列表 将前台提交的明细数组转换成json 以name值为键 对应输入框值
        $json_detaile = $request->input('r_detaile_list','{}');
        $params['r_detaile_list'] = json_encode($json_detaile);
        //写入房源之前验证房源是否重复
        $res = $RoomInfo->verificationRoomRepeat($params);
        if(!$res){
            return $this->error(Error::ADD_ERROR);
        }

        $res = $RoomInfo->addRoomInfo($params);
        // dd($res);
        if (!$res) {
            //添加失败
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功');
    }
}

